package org.brisling.common.service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.util.FileUtil;
import org.brisling.common.util.JsonUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <p>
 * Title: GenerateService
 * </p>
 * <p>
 * Description: 自动生成文件模块
 * </p>
 * <p>
 * Company: tongking
 * </p>
 * 
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年4月18日 下午2:28:21
 */
@RestController
@RequestMapping("/generate")
public class GenerateService {

	/**
	 * 日志处理器
	 */
	private final static Log log = LogFactory.getLog(GenerateService.class);

	private String tempPath = "d:/upload/config";

	private String outPath = "/WEB-INF/html";

	private String page = "login";

	public GenerateService() {
	}

	/**
	 * 生成登录页
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/loginPage.action")
	public RetDomain getLoginPage(@RequestParam(value = "site", required = false) String site,

			HttpServletRequest request) {

		if (site != null && page != null) {
			String realPath = request.getSession().getServletContext().getRealPath(outPath);

			log.info("ServletPath: " + request.getServletPath());
			log.info("RemoteAddr: " + request.getRemoteAddr());
			log.info("getRemoteHost: " + request.getRemoteHost());
			log.info("getRemoteUser: " + request.getRemoteUser());
			log.info("RealPath: " + request.getSession().getServletContext().getRealPath(outPath));
			log.info("RemoteAddr: " + request.getRemoteAddr());
			log.info("getRemoteHost: " + request.getRemoteHost());
			log.info("getRemoteUser: " + request.getRemoteUser());
			// 模板文件路径
			String tplFilepath = String.format("%1$s/%2$s/%3$s.tpl", tempPath, site, page);
			;

			// 变量文件路径
			String variablePath = String.format("%1$s/%2$s/%3$s.var", tempPath, site, page);

			// 输出文件路径
			String outFilepath = String.format("%1$s/%2$s/%3$s.html", realPath, site, page);
			String pageVar = FileUtil.readFile(variablePath, null);

			LinkedHashMap<String, Object> map = JsonUtil.body2Class(pageVar, LinkedHashMap.class);

			FileUtil.exportWord(map, tplFilepath, outFilepath);

			return new RetDomain();
		} else {
			return RetDomain.errorDomain("页面生成失败");
		}

	}

	@RequestMapping("/generateAllPage.action")
	public RetDomain generateAll(@RequestParam(value = "pagecode", required = false)String pagecode,
			HttpServletRequest request) {

		RetDomain ret = new RetDomain();
		String realPath = request.getSession().getServletContext().getRealPath(outPath);

		String pageConfigPath = tempPath + "/page.var";

		// 读取页面配置文件
		String pageConfigVar = FileUtil.readFile(pageConfigPath, null);
		Map<String, Object> pageMap = JsonUtil.body2Class(pageConfigVar, HashMap.class);

		if (pageMap != null && pageMap.containsKey("pageConfig")) {
			Object oc = pageMap.get("pageConfig");

			List<Object> lc = null;
			if (oc instanceof List) {
				lc = (List<Object>) oc;
			}
			// 循环生成文件
			for (Object obj : lc) {
				if (obj != null) {
					Map<String, Object> pm = (Map<String, Object>) obj;
					if (pm != null) {

						// 判断页面配置是否存在
						if (pm.containsKey("page")) {
							Object po = pm.get("page");
							if (po != null) {

								// 判断是否制定页面代码
								if (pagecode != null) {
									// 指定页面，判断当前页面代码与指定页面代码是否相等
									String pcode = pm.containsKey("code")
											? (pm.get("code") == null ? null : pm.get("code").toString()) : null;
									if (pagecode.compareTo(pcode) == 0) {
										generatePage(tempPath + po.toString(),realPath);
									}
								} else {
									generatePage(tempPath + po.toString(),realPath);
								}
							}
						}
					}
				}
			}
		}
		
		return ret;

	}

	private void generatePage(String page,String realPath) {

		// 模板文件路径
		String tplFilepath = page + ".tpl";

		// 变量文件路径
		String variablePath = page + ".var";

		// 读取变量值
		String pageVar = FileUtil.readFile(variablePath, null);
		Map<String, Object> map = JsonUtil.body2Class(pageVar, HashMap.class);

		// 判断配置有效性
		if (map != null) {
			if (map.containsKey("out") && (map.containsKey("data"))) {

				System.out.println("page: " + page);
				// 输出文件路径
				String outpath = map.get("out").toString();
				// String outFilepath = String.format(outpath+".html");

				String outFilepath = String.format(realPath+outpath + ".html");

				Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
				FileUtil.exportWord(dataMap, tplFilepath, outFilepath);
			}
		}

	}

}
